package xgame.login.handler;

import xgame.AbstractXhandler;
import xgame.login.bizservice.Services;
import xgame.login.cmd.LoginCmd;
import xgame.login.result.LoginResult;

/**
 * 登录命令处理器
 * 
 * @author AfritXia 
 * @version $Rev: 0 $
 *
 */
public class LoginHandler extends AbstractXhandler<LoginCmd> {
	/**
	 * 用户 Id
	 * 
	 */
	private static final String USER_ID = "__userId";

	/**
	 * 用户名称
	 * 
	 */
	private static final String USER_NAME = "__userName";
	
	@Override
	public void handle() {
		// 获取登录命令
		LoginCmd cmd = this.getCommand();

		String userName = cmd.getUserName();
		String userPass = cmd.getUserPass();

		// 登录
		boolean loginOk = Services.getLoginService().login(userName, userPass);

		if (loginOk) {
			// 设置用户 Id 和名称
			this.getSession().putAttribute(USER_ID, System.currentTimeMillis());
			this.getSession().putAttribute(USER_NAME, userName);
		}

		// 写出登录结果
		this.getOutput().write(new LoginResult(loginOk));
	}
}
